package zjh_20230406;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        try {
            InetAddress localHost = InetAddress.getLocalHost();
            int port = 8000;
            Boolean flag;
            while (flag = true){
            Scanner scanner = new Scanner(System.in);
            System.out.println("输入聊天内容: ");
            String next = scanner.next();
            String str = "end";

            DatagramPacket datagramPacket = new DatagramPacket(next.getBytes(),next.length(),localHost,port);

                if (next.equals(str)){
                    flag = false;
                    DatagramSocket datagramSocket = new DatagramSocket(8080,localHost);
                    datagramSocket.send(datagramPacket);
                    datagramSocket.close();
                    System.out.println("发送成功");
                }
            }
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (SocketException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
